let utils = require('../../utils/util')
//初始化云数据库环境
let db = wx.cloud.database({
    evn: 'linyr-6gp7qo8008360a60'
})
let pet = db.collection('db_pet')

let look = db.collection('db_look')


Page({

    /**
     * 页面的初始数据
     */
    data: {
        classifylist: [],
        titlelist: [{
                type: 0,
                img: 'cloud://linyr-6gp7qo8008360a60.6c69-linyr-6gp7qo8008360a60-1317695142/6.jpg',
                name: '猫类',
                ename: 'Cats'
            },
            {
                type: 1,
                img: 'cloud://linyr-6gp7qo8008360a60.6c69-linyr-6gp7qo8008360a60-1317695142/3.jpg',
                name: '狗类',
                ename: 'Dogs'
            },
            {
                type: 2,
                img: 'cloud://linyr-6gp7qo8008360a60.6c69-linyr-6gp7qo8008360a60-1317695142/9.jpg',
                name: '爬行类',
                ename: 'Crawling class'
            },
            {
                type: 3,
                img: 'cloud://linyr-6gp7qo8008360a60.6c69-linyr-6gp7qo8008360a60-1317695142/7.jpg',
                name: '小宠物类',
                ename: 'Small pets'
            },
            {
                type: 4,
                img: 'cloud://linyr-6gp7qo8008360a60.6c69-linyr-6gp7qo8008360a60-1317695142/10.jpg',
                name: '水生类',
                ename: 'Aquatic'
            }
        ],
        titleIndex: '',
        count: 0
    },

    //去列表详情
    //  goClassifyList(event){
    //     // console.log(event);
    //     if(event.currentTarget.dataset.type){
    //         let type = event.currentTarget.dataset.type
    //         console.log(type);
    //         pet.where({pet_type:Number(type)}).get().then(content=>{
    //             console.log(content);
    //         })
    //     }
    // },
    //去详情页
    async goDetail(event) {
        // console.log(event);
        let _id = event.currentTarget.dataset.id
        let dt = new Date()
        let look_time = utils.formatTime(dt)
        await look.where({
            look_id: _id
        }).get().then(content => {
            // console.log(content);
            let {
                data,
                errMsg
            } = content
            if (errMsg == 'collection.get:ok') {
                if (data.length == 0) {
                    pet.where({
                        _id
                    }).get().then(content => {
                        console.log('2.0', content);
                        let {
                            data,
                            errMsg
                        } = content
                        if (errMsg == 'collection.get:ok') {
                            let look_img = data[0].pet_coverURL
                            let look_name = data[0].pet_name
                            look.add({
                                data: {
                                    look_id: _id,
                                    look_time,
                                    look_img,
                                    look_name
                                }
                            })
                        }
                    })
                }
            }
        })
        wx.navigateTo({
            url: '../detail/detail?id=' + _id,
        })
    },

    async init(options) {
        wx.showLoading({
            title: '加载中...',
            icon: "none"
        })
        this.setData({
            titleIndex: options.pet_type
        })
        await pet.where({
            pet_type: Number(options.pet_type)
        }).limit(5).get().then(content => {
            // console.log(content);
            let {
                data,
                errMsg
            } = content
            if (errMsg == 'collection.get:ok') {
                this.setData({
                    classifylist: data
                })
                wx.hideLoading();
            }
            // console.log(this.data.classifylist);
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(options.pet_type);
        this.init(options);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        pet.where({
            pet_type: Number(this.data.titleIndex)
        }).count().then(content => {
            // console.log(content);
            let {
                total,
                errMsg
            } = content
            if (errMsg == "collection.count:ok") {
                this.setData({
                    count: total
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    async onReachBottom() {
        let length = this.data.classifylist.length
        if (length < this.data.count) {
            wx.showLoading({
                title: '加载中',
            })
            await pet.where({
                pet_type: Number(this.data.titleIndex)
            }).skip(length).limit(5).get().then(content => {
                let {
                    data,
                    errMsg
                } = content
                if (errMsg == 'collection.get:ok') {
                    let arr = this.data.classifylist.concat(data)
                    this.setData({
                        classifylist: arr
                    })
                    wx.hideLoading();
                }
            })
        }else{
            wx.showToast({
              title: '已经到底了',
              icon:"none"
            })
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})